package com.imagination.applet.controller.api;

import com.imagination.applet.controller.api.request.DeleteProductImageRequest;
import com.imagination.applet.controller.view.ProductImageVO;
import com.imagination.applet.domain.product.model.ProductImage;
import com.imagination.applet.domain.product.service.ProductImageCommandService;
import com.imagination.applet.domain.product.service.ProductImageQueryService;
import com.imagination.applet.infrastructure.common.RestResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/product/images")
public class ProductImageController {
    private final ProductImageCommandService productImageCommandService;
    private final ProductImageQueryService productImageQueryService;

    @GetMapping
    public RestResponse<Page<ProductImageVO>> queryProductImage(Pageable pageable,String channelTypes) {
        String[] split = channelTypes.split(",");
        Page<ProductImage> paged = productImageQueryService.queryByPageable(pageable,Arrays.asList(split));
        Page<ProductImageVO> result = paged.map(p -> ProductImageVO.builder()
                .productImageId(p.getId())
                .taskId(p.getTaskId())
                .aiImageId(p.getAiImageId())
                .isHd(p.getIsHd())
                .virtualIdentityId(p.getVirtualIdentityId())
                .trainingStatus(p.getTrainingStatus())
                .link(p.getLink())
                .build()
        );
        return RestResponse.ok(result);
    }


    @GetMapping("queryCount")
    public RestResponse<Integer> queryCount(String channelTypes) {
        String[] split = channelTypes.split(",");
        Integer count = productImageQueryService.queryCount(Arrays.asList(split));
        return RestResponse.ok(count);
    }

    @DeleteMapping
    public RestResponse<Boolean> deleteProductImage(DeleteProductImageRequest request) {
        productImageCommandService.deleteImagesById(request.getProductIdList());
        return RestResponse.ok(true);
    }

}
